package com.example.loader;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.topic.ITopic;
import com.hazelcast.topic.Message;
import com.hazelcast.topic.MessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/*
 * @className: HazelcastTopicLoader
 * @author: chentao
 * @date: 2025/2/19 上午9:00
 * @Version: 1.0
 * @description:
 */
@Service
public class HazelcastTopicLoader implements HazelcastLoader {

    Logger logger = LoggerFactory.getLogger(getClass());

    public static final String Topic = "Topic_Topic_Topic_Topic";

    private ITopic<String> topic;

    @Override
    public void init(HazelcastInstance hazelcastInstance) throws Exception {
        this.topic = hazelcastInstance.getTopic(Topic);
        this.topic.addMessageListener(new MessageListener<String>() {
            @Override
            public void onMessage(Message<String> message) {
                logger.warn("topic收到消息: " + message);
            }
        });
    }

    public ITopic<String> getTopic() {
        return topic;
    }
}
